home *** CD-ROM | disk | FTP | other *** search
- /***************************************
- cDotsTask.c
-
- SUPERCLASS = CMouseTask
-
- Handles mouse clicks in the dots pane.
- Also provides undo/redo capability.
-
- ****************************************/
-
- #include "cDotsTask.h"
- #include "dotsTypes.h"
-
- /******** C O N S T R U C T I O N ********/
-
- /*** IDotsTask
- *
- * Initialize a DotsTask object
- */
- void cDotsTask::IDotsTask(
- short aNameIndex,
- cDotsPane *theDotsPane,
- cDotsDoc *theDotsDoc)
- {
- inherited::IMouseTask(aNameIndex);
-
- /* Initialize instance variables */
- fDotsPane = theDotsPane;
- fDotsDoc = theDotsDoc;
- fPlayerMoved = fDotsDoc->getPlayerTurn(); /* Player whose move it is */
- fValidMove = false; /* Only valid if a previously unselected
- ** line is selected by this mouse task */
- fDirection = hLine;
- fRow = -1;
- fCol = -1;
- }
-
- /***** M O U S E T R A C K I N G ********/
-
- /*** BeginTracking {OVERRIDE}
- *
- * Mouse tracking starting. Adjust starting pt.
- */
- void cDotsTask::BeginTracking(
- Point *startPt)
- {
-
- tLineDir direction;
- int row;
- int col;
- Rect r;
-
- /* See if pt is on a line */
- if (fDotsPane->pt2Line(*startPt, &direction, &row, &col)) {
- /* Convert line coords to a pt on the grid */
- fDotsPane->line2Pt(direction, row, col, startPt);
- /* If line unoccupied then fill it */
- if (!fDotsDoc->getLineState(direction, row, col)) {
- fDotsPane->line2Rect(direction, row, col, &r);
- FillRect(&r, black);
- }
- }
- }
-
-
- /*** KeepTracking {OVERRIDE}
- *
- * Continuous mouse tracking while the mouse button is down. Draw
- * only when the mouse moves or the panorama autoscrolls.
- */
- void cDotsTask::KeepTracking(
- Point *currPt,
- Point *prevPt,
- Point *startPt)
- {
-
- int col;
- tLineDir direction;
- Boolean eraseIt;
- Rect r;
- int row;
-
- /* Only track if not autoScrolling */
- if (!fDotsPane->AutoScroll(*currPt)) {
-
- /* assume won't erase line previously chosen
- ** by this mouse down */
- eraseIt = false;
-
- /* Check for current pt on a line */
- if (fDotsPane->pt2Line(*currPt, &direction, &row, &col)) {
- /* Convert line coords to a pt on the grid */
- fDotsPane->line2Pt(direction, row, col, currPt);
- /* Check for new chosen line */
- if (!EqualPt(*currPt, *startPt)) {
- /* If new chosen line unoccupied then fill it */
- if (!fDotsDoc->getLineState(direction, row, col)) {
- fDotsPane->line2Rect(direction, row, col, &r);
- FillRect(&r, black);
- }
- eraseIt = true; /* erase previous line */
- }
- } else
- eraseIt = true; /* Erase any previously chosen line */
-
- /* If no longer over a previously chosen line then erase it */
- if (eraseIt) {
- /* Check if start pt on a line */
- if (fDotsPane->pt2Line(*startPt, &direction, &row, &col)) {
- /* Only erase if start line is unoccupied */
- if (!fDotsDoc->getLineState(direction, row, col)) {
- /* erase the previouly chosen line */
- fDotsPane->line2Rect(direction, row, col, &r);
- FillRect(&r, white);
- /* redraw both dots */
- fDotsPane->dot2Rect(row, col, &r);
- FillOval(&r, black);
- if (direction == hLine)
- col++;
- else
- row++;
- fDotsPane->dot2Rect(row, col, &r);
- FillOval(&r, black);
- }
- }
- /* Reset startPt to new point */
- SetPt(startPt, currPt->h, currPt->v);
- }
- }
- }
-
-
- /*** EndTracking {OVERRIDE}
- *
- * Mouse down tracking has ended because the user has released the
- * mouse button. Its now time to complete the move and set up the undo.
- */
- void cDotsTask::EndTracking(
- Point *currPt,
- Point *prevPt,
- Point *startPt)
- {
- tLineDir direction;
- int row;
- int col;
-
- /* The only valid move is the selection of a previously
- ** unselected line so check if selected point is on line
- ** and the line is unoccupied */
- if ((fDotsPane->pt2Line(*prevPt, &direction, &row, &col)) &&
- (!fDotsDoc->getLineState(direction, row, col))) {
- fValidMove = true;
- fDirection = direction;
- fRow = row;
- fCol = col;
-
- Do(); /* Act on users choice */
- }
- }
-
- /*********** D O / U N D O ***********/
-
- /*** Do {OVERRIDE}
- *
- * Perform a dots pane task, user chose a line
- */
- void cDotsTask::Do()
- {
- fDotsDoc->setLineState(fDirection, fRow, fCol, fPlayerMoved, true);
- }
-
- /*** Undo {OVERRIDE}
- *
- * Undo an action performed by a dots pane task
- */
- void cDotsTask::Undo()
- {
- fDotsDoc->setLineState(fDirection, fRow, fCol, fPlayerMoved, false);
- }
-
- /*** Redo {OVERRIDE}
- *
- * Redo an action performed by a dots pane task
- */
- void cDotsTask::Redo()
- {
- fDotsDoc->setLineState(fDirection, fRow, fCol, fPlayerMoved, true);
- }
-
- /*********** A C C E S S ***********/
-
- /*** validMove
- *
- * Return value of fValidMove instance variable.
- * The value will be true if a previously unselected line was selected
- * during this mouse task, false otherwise.
- */
- Boolean cDotsTask::validMove()
- {
- return(fValidMove);
- }
-